package kr.co.casually;

import kr.co.casually.notification.ConcertInfoService;
import kr.co.casually.preference.ConcertPreferences;
import kr.co.casually.util.TabsUtil;
import kr.co.casually.util.Utils;
import android.app.Dialog;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
	private TabHost mTabHost;
	public static ImageLoader imageLoader;
	NotificationManager notificationManager;
	private SharedPreferences mPrefs;

	private ProgressDialog mProgressDialog;
	private static final int MAX_PROGRESS = 100;
	private static final int DIALOG_PROGRESS = 1;
	private int mProgress;
	public static Handler mProgressHandler;
	private final int artCodeCnt = 16;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_main_act);

		initActivity();
	}

	private void initActivity() {
		imageLoader = new ImageLoader(this);
		mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		initTabHost();

		if (!mPrefs.getBoolean(ConcertPreferences.FIRST_SERVICE, false)) {
			firstProgress();
			firstServiceRun();
		}

	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_PROGRESS:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setTitle(R.string.creating_db);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgressDialog.setMax(MAX_PROGRESS);
			mProgressDialog.setProgress(0);
			mProgressDialog.setCancelable(false);
		}
		return mProgressDialog;

	}

	private void initTabHost() {
		mTabHost = getTabHost();
		mTabHost.setBackgroundColor(Color.WHITE);
		TabsUtil.addTab(mTabHost, getString(R.string.my_concert_list_info),
				R.drawable.tab_main_nav_my_concert_list_selector, 1,
				new Intent(this, MyConcertListActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		TabsUtil.addTab(mTabHost, getString(R.string.concert_info),
				R.drawable.tab_main_nav_concert_selector, 2, new Intent(this,
						ConcertActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		TabsUtil.addTab(mTabHost, getString(R.string.setting),
				R.drawable.tab_main_nav_setting_selector, 3, new Intent(this,
						SettingActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

	}

	// 최초 1회 DB 생성 프로그레스 다이얼로그 실행
	private void firstProgress() {
		mProgress = 0;
		mProgressHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				if (mProgress >= MAX_PROGRESS) {
					mProgressDialog.dismiss();
					Utils.CREATE_DB_INCREMENT = 0;
				} else {
					if (Utils.CREATE_DB_INCREMENT == 0) {
						mProgress = 3;
					} else {
						mProgress = Utils.CREATE_DB_INCREMENT * 100
								/ artCodeCnt;
					}
					mProgressDialog.setProgress(mProgress);
					if (mProgress >= MAX_PROGRESS) {
						mProgressDialog.dismiss();
						Utils.CREATE_DB_INCREMENT = 0;

					}
				}

			}
		};

		showDialog(DIALOG_PROGRESS);
		mProgressHandler.sendEmptyMessage(0);

	}

	// 최초 1회 서비스 실행..
	private void firstServiceRun() {
		String svcName = Context.NOTIFICATION_SERVICE;
		notificationManager = (NotificationManager) getSystemService(svcName);
		startService(new Intent(this, ConcertInfoService.class));
	}

}
